package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.dao.seat.GetSeatTime;
import com.user.dao.seat.SeatDao;
import com.user.dao.seat.SeatTotal;
import com.user.dao.seat.test;
import com.user.domain.Order;
import com.user.domain.Seats;
import com.user.mapper.OrderMapper;
import com.user.mapper.SeatsMapper;
import com.user.service.SeatsService;
import com.user.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YU
 * @description 针对表【seats】的数据库操作Service实现
 * @createDate 2024-03-09 13:23:15
 */
@Service
public class SeatsServiceImpl extends ServiceImpl<SeatsMapper, Seats> implements SeatsService {
    @Autowired
    SeatsMapper seatsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public R getSeat(GetSeatTime seat) {
        //查询Reservations里包含seats集合的seatid
        List<Integer> seatTimes = orderMapper.getSeatIdsByTime(seat.getRestaurantId());
        //根据餐厅id查询座位信息
        LambdaQueryWrapper<Seats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Seats::getRestaurantId, seat.getRestaurantId())
                .eq(Seats::getSeatStatus, 1);
        if (!seatTimes.isEmpty()) wrapper.notIn(Seats::getId, seatTimes);
        List<Seats> seats = list(wrapper);
        if (seats.isEmpty()) return R.error("没有座位信息");
        List<test> tests = new ArrayList<>();
        for (Seats seat1 : seats) {
            test test = new test();
            BeanUtils.copyProperties(seat1, test);
            List<Integer> seatTime = orderMapper.getSeatTime(seat1.getId(), seat.getTime());
            List<Integer> time = Arrays.asList(1, 2, 3, 4);
            //移除已经被预定的时间段
            if (seatTime != null) {
                List<Integer> modifiableTime = new ArrayList<>(time);
                modifiableTime.removeAll(seatTime);
                time = modifiableTime;
            }
            test.setTime(time);
            tests.add(test);
        }
        return R.ok().setData(tests);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R createSeat(SeatDao seat) {
        if (seat == null) return R.error("座位信息为空");
        //根据餐厅id修改座位信息
        Seats seats = new Seats();
        //拷贝属性
        BeanUtils.copyProperties(seat, seats);
        seats.setSeatStatus(0);
        saveOrUpdate(seats);
        return R.ok();
    }

    @Override
    public R getSeatList(Integer restaurantId) {
        if (restaurantId == null) return R.error("座位信息为空");
        //根据餐厅id查询座位信息
        LambdaQueryWrapper<Seats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Seats::getRestaurantId, restaurantId)
                .ne(Seats::getSeatStatus, 2);
        List<Seats> seats = list(wrapper);
        if (seats.isEmpty()) return R.error("没有座位信息");
        return R.ok().setData(seats);
    }

    @Transactional
    @Override
    public R updateStatus(Integer seatId) {
        if (seatId == null) return R.error("座位信息为空");
        //根据座位id修改座位状态
        Seats seats = getById(seatId);
        if (seats == null) return R.error("没有座位信息");
        if (seats.getSeatStatus() == 2) return R.error("座位已删除");
        if (seats.getSeatStatus() == 0) seats.setSeatStatus(1);
        else seats.setSeatStatus(0);
        saveOrUpdate(seats);
        return R.ok();
    }

    @Transactional
    @Override
    public R deleteSeat(Integer seatId) {
        if (seatId == null) return R.error("座位信息为空");

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getSeatId, seatId)
                .in(Order::getStatus, 0, 1);
        boolean exists = orderMapper.selectCount(wrapper) > 0;
        if (exists) return R.error("座位已被预定");

        //根据座位id删除座位信息
        Seats seats = getById(seatId);
        if (seats == null) return R.error("没有座位信息");
        seats.setSeatStatus(2);
        saveOrUpdate(seats);
        return R.ok();
    }

    @Override
    public R getIdleSeats(Integer restaurantId) {
        if (restaurantId == null) return R.error("座位信息为空");
        //获取当前时间和7天后的时间
        Date date = new Date();
        Date date1 = new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000);
        //查询7太内的订单，排除取消和评价的订单
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getRestaurantId, restaurantId)
                .between(Order::getDatePeriod, date, date1)
                .ne(Order::getStatus, 2)
                .ne(Order::getStatus, 3);
        List<Order> orders = orderMapper.selectList(wrapper);
        if (orders.isEmpty()) return R.error("没有座位信息");

        return null;
    }

    @Override
    public R getSeatTotal(Integer restaurantId) {
        if (restaurantId == null) return R.error("座位信息为空");
        List<Integer> ids = orderMapper.selectSeatTotal(restaurantId);
        if (ids.isEmpty()) return R.error("没有座位信息");

        Map<Integer, Long> seatCountMap = ids.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        List<SeatTotal> seatTotals = seatCountMap.entrySet().stream()
                .map(entry -> {
                    SeatTotal seatTotal = new SeatTotal();
                    seatTotal.setSeatName(seatsMapper.selectNameByid(entry.getKey()));
                    seatTotal.setCount(entry.getValue().intValue());
                    return seatTotal;
                })
                .collect(Collectors.toList());

        return R.ok().setData(seatTotals);
    }
}




